﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Styx;
using System.Windows.Forms;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Profiles;

namespace RoutesAutomator
{
    public class BotAction:IDisposable
    {
        public static class BotName
        {
            public const string Gatherbuddy2 = "Gatherbuddy2";

        }
        static bool _botIsChanging;
        static public void ChangeBot(string name)
        {
            if (_botIsChanging)
            {
                //Professionbuddy.Log("Must wait for previous ChangeBot to finish before calling ChangeBot again.");
                return;
            }
            BotBase bot = BotManager.Instance.Bots.FirstOrDefault(b => b.Key.Contains(name)).Value;
            if (BotManager.Current == bot)
                return;

            if (bot != null)
            {
                // execute from GUI thread since this thread will get aborted when switching bot
                _botIsChanging = true;
                BotManager.Instance.SetCurrent(bot);
                _botIsChanging = false;
            }
            else
            {
                MessageBox.Show("There was a problem setting the bot.");
                //Professionbuddy.Err("Bot {0} does not exist", name);
            }
        }

        static public void StartBot()
        {
            IsBotRunning = true;
        }
        static public void StopBot()
        {
            IsBotRunning = false;
        }
        static public bool IsBotRunning
        {
            get { return TreeRoot.IsRunning; }
            private set 
            {
                if (!TreeRoot.IsRunning && value)
                    TreeRoot.Start();
                else if (TreeRoot.IsRunning && !value)
                    TreeRoot.Stop();

            }
        }

        static public void LoadProfile(string pathProfileFileName)
        {
            if (!string.IsNullOrEmpty(pathProfileFileName))
            {
                ProfileManager.LoadNew(pathProfileFileName);
                return;
            }
            
            if (ProfileManager.CurrentProfile == null)
            {   
                    ProfileManager.LoadEmpty();
            }
        }
        public void Dispose()
        {
            _botIsChanging = false;
        }
    }

}
